#include "loginwidget.h"
#include "ui_loginwidget.h"
#include "registerwidget.h"
#include "forgotwidget.h"
#include <QMessageBox>
#include <QDebug>

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget), m_pRegist(new RegisterWidget()),
    m_pForgot(new ForgotWidget()) {
    ui->setupUi(this);

    ui->btnLogin->setDefault(true);
    setupButtonStyleLeft(ui->btnLoginToRegister);
    setupButtonStyleRight(ui->btnForgotPass);

    this->show();
    m_pRegist->hide();
    m_pForgot->hide();

    connect(ui->btnLoginToRegister, &QPushButton::clicked, this, &LoginWidget::switchToRegister);
    connect(ui->btnForgotPass, &QPushButton::clicked, this, &LoginWidget::switchToForgot);
    connect(m_pRegist, &RegisterWidget::returnToLogin, this, &LoginWidget::returnToLogin);
    connect(ui->btnLogin, &QPushButton::clicked, this, &LoginWidget::onLoginClicked);

}

void LoginWidget::switchToRegister() {
    this->hide();
    m_pRegist->show();
    m_pRegist->adjustSize();
}

void LoginWidget::switchToForgot() {
    this->hide();
    m_pForgot->show();
    m_pForgot->adjustSize();
}

void LoginWidget::returnToLogin() {
    m_pRegist->hide();
    m_pForgot->hide();
    this->show();
    ui->txtUsername->clear();
    ui->txtPassword->clear();
    this->adjustSize();
}

void LoginWidget::onLoginClicked() {
    QString tempUsername = ui->txtUsername->text();
    QString tempPassword = ui->txtPassword->text();
    emit loginRequested(tempUsername, tempPassword);
}

void LoginWidget::onReceiveLogin(bool success) {
    qDebug() << "Received login result: " << success;
    QString tempUsername = ui->txtUsername->text();
    if(success) {
        QMetaObject::invokeMethod(this, [=]() {
            ui->txtPassword->clear();
            this->close();
            if(ui->txtUsername->text() == "admin_user"){
                emit onAdminSuccess();
            } else {
                emit onUserSuccess();
            }
            emit onUserName(tempUsername);
            QMessageBox::information(nullptr, "登录成功", "欢迎登录！");
        }, Qt::QueuedConnection);
    } else {
        QMessageBox::warning(nullptr, "登录失败", "账号或密码错误，请重试。");
    }
}

void LoginWidget::switchToLoginPage() {
    m_pRegist->hide();
    m_pForgot->hide();
    this->show();
    this->adjustSize();
}

void LoginWidget::setupButtonStyleLeft(QPushButton *button)
{
    button->setStyleSheet(
        "QPushButton {"
        "border: none; /* 无边框 */"
        "text-align: left;"
        "color: blue; /* 正常状态下文字颜色 */"
        "}"
        "QPushButton:hover {"
        "color: red; /* 悬浮状态下文字颜色 */"
        "}"
    );
}

void LoginWidget::setupButtonStyleRight(QPushButton *button)
{
    button->setStyleSheet(
        "QPushButton {"
        "border: none; /* 无边框 */"
        "text-align: right;"
        "color: blue; /* 正常状态下文字颜色 */"
        "}"
        "QPushButton:hover {"
        "color: red; /* 悬浮状态下文字颜色 */"
        "}"
    );
}

LoginWidget::~LoginWidget() {

    delete ui;
}


